const initialState = {
  userChannelList: [] as ChannelType[],
  allChannelList: [] as ChannelType[],
};
export type AddChannelType = { channels: { id: number; name: string }[] };

export type ChannelType = { id: number; name: string };
type ActionType =
  | { type: 'chennel/userChannel'; payload: ChannelType[] }
  | { type: 'chennel/allChannel'; payload: ChannelType[] }
  | { type: 'channel/delUserChannel'; payload: ChannelType }
  | { type: 'channel/addUserChannel'; payload: ChannelType };

export default function channelReducer(
  state = initialState,
  action: ActionType
) {
  switch (action.type) {
    case 'chennel/userChannel':
      return {
        ...state,
        userChannelList: action.payload,
      };
    case 'chennel/allChannel':
      return {
        ...state,
        allChannelList: action.payload,
      };
    case 'channel/delUserChannel':
      return {
        ...state,
        userChannelList: state.userChannelList.filter(
          (item) => item.id !== action.payload.id
        ),
      };
    case 'channel/addUserChannel':
      return {
        ...state,
        userChannelList: [...state.userChannelList, action.payload],
      };
    default:
      return state;
  }
}
